<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 组合式api和选项式api在html部分是完全相同的 -->
      <h1>todolist</h1>
      <input type="text" v-model="value" />
      <button @click="add">添加</button>

      <ul>
        <li v-for="item in list" :key="item.id">
          {{item.name}}
          <button @click="remove(item.id)">删除</button>
        </li>
      </ul>

      <hr />

      <p>count: {{count}}</p>
      <button @click="increment">count++</button>

      <hr />

      <ul ref="ul">
        <li v-for="item in arr" :key="item.id">{{ item.nm }}</li>
      </ul>
    </div>

    <script src="../Vue.js"></script>

    <script>
      const { createApp, ref, onMounted } = Vue;

      createApp({
        // setup是一个最早执行的生命周期
        // 有了setup就不需要其他的选项了
        setup() {
          // 用ref来定义响应式变量
          const count = ref(10);
          const increment = () => {
            // console.log(count);
            count.value++;
          };

          const value = ref("");
          const list = ref([]);
          const add = () => {
            list.value.push({ id: new Date().getTime(), name: value.value });
            value.value = "";
          };
          const remove = (id) => {
            list.value = list.value.filter((item) => item.id !== id);
          };

          const arr = ref([]);
          onMounted(() => {
            fetch("http://101.35.148.193:3000/api/rated/list")
              .then((response) => response.json())
              .then((res) => {
                arr.value = res.data;
              });
          });

          // return的内容就可以给template进行使用
          return { count, increment, value, list, add, remove, arr };
        },
      }).mount("#app");
    </script>
  </body>
</html>
